/**
 * 作者: wgx
 * 日期: 2025-02-24 10:41:33
 * 备注: 怪兽洞窟的本服坑位占领人的信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameCave.PBServerCaveLocation;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class ServerCaveLocationModel extends BaseModel implements Model {
    public static final String LISTKEY = "server_id.location_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库ID
    protected int serverId; // 服务器id
    protected int locationId; // 坑位Id, (区域Id和层id组成的唯一值)
    protected long userId; // 占领者的userid
    protected String userName = ""; // 占领者的user_name
    protected long power; // 占领时的战力
    protected String partner = ""; // 占领时的干员快照
    protected String icon = ""; // 占领者的icon

    public void reset() {
    }

    protected String getKey() {
        return "ServerCaveLocation." + serverId + "." + locationId;
    }

    protected String getListKey() {
        return "ServerCaveLocation." + serverId;
    }

    protected String getListItemKey() {
        return locationId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("server_cave_location"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库ID
        obj.put("serverId", serverId); // 服务器id
        obj.put("locationId", locationId); // 坑位Id, (区域Id和层id组成的唯一值)
        obj.put("userId", userId); // 占领者的userid
        obj.put("userName", userName); // 占领者的user_name
        obj.put("power", power); // 占领时的战力
        obj.put("partner", partner); // 占领时的干员快照
        obj.put("icon", icon); // 占领者的icon
        return obj;
    }

    public PBServerCaveLocation.Builder takeInitBuilder() {
        PBServerCaveLocation.Builder sb = PBServerCaveLocation.newBuilder();
        if (locationId != 0) {
            sb.setLocationId(locationId);
        }
        if (userId != 0) {
            sb.setUserId(userId);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (power != 0) {
            sb.setPower(power);
        }
        if (!partner.isEmpty()) {
            sb.setPartner(partner);
        }
        if (!icon.isEmpty()) {
            sb.setIcon(icon);
        }
        return sb;
    }

    public PBServerCaveLocation.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBServerCaveLocation.Builder sb = PBServerCaveLocation.newBuilder();
        if (isFieldUpdate("locationId")) {
            sb.setLocationId(locationId);
        }
        if (isFieldUpdate("userId")) {
            sb.setUserId(userId);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("power")) {
            sb.setPower(power);
        }
        if (isFieldUpdate("partner")) {
            sb.setPartner(partner);
        }
        if (isFieldUpdate("icon")) {
            sb.setIcon(icon);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public int getLocationId() {
        return locationId;
    }

    public void putLocationId(int locationId) {
        if (this.locationId != locationId) {
            this.locationId = locationId;
            updateRedisHashField(getKey(), "locationId", this.locationId);
        }
    }

    public void addLocationId(int value) {
        this.locationId += value;
        updateRedisHashField(getKey(), "locationId", this.locationId);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public long getPower() {
        return power;
    }

    public void putPower(long power) {
        if (this.power != power) {
            this.power = power;
            updateRedisHashField(getKey(), "power", this.power);
        }
    }

    public void addPower(long value) {
        this.power += value;
        updateRedisHashField(getKey(), "power", this.power);
    }

    public String getPartner() {
        return partner;
    }

    public void putPartner(String partner) {
        if (!this.partner.equals(partner)) {
            this.partner = partner;
            updateRedisHashField(getKey(), "partner", this.partner);
        }
    }

    public String getIcon() {
        return icon;
    }

    public void putIcon(String icon) {
        if (!this.icon.equals(icon)) {
            this.icon = icon;
            updateRedisHashField(getKey(), "icon", this.icon);
        }
    }

}